package com.rzjm.api.util;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

public class StringUtil {
    /**
     * 将以逗号分隔的字符串转换成字符串数组
     * @param valStr
     * @return String[]
     */
    public static String[] StrT0List(String valStr,String code){
        String[] array = new String[0];
        if(valStr != null &&!valStr.equals("")){
            if(valStr.contains(code)){
                array = valStr.split(code);
            }else{
                array = new String[1];
                array[0] = valStr;
            }
        }
        return array;
    }


    /**
     *  右补零
     * @param str
     * @param targetLength
     * @return
     */
    public static String strRightPadding(String str, int targetLength){
        String ss = String.format("%-" + targetLength + "s",  str).replace(" ", "0");
        return ss;
    }

    /**
     * 分转元（保留两位有效数字）
     * @param amount
     * @return
     */
    public static String centToYuan(String amount){
        DecimalFormat df = new DecimalFormat("######0.00");
        Double d = Double.parseDouble(amount)/100;
        return df.format(d);
    }
    /**
     * 元转分
     * @param amount
     * @return
     */
    public static String yuanToCent(String amount){
        DecimalFormat df = new DecimalFormat("######0");
        Double d = Double.parseDouble(amount) * 100;
        return df.format(d).toString();
    }

    /**
     *  字符串反转
     * @param s
     * @return
     */
    public static String reverseTestSeven(String s) {

        int length = s.length();
        if (length <= 1) return s;
        String left = s.substring(0, length / 2);
        String right = s.substring(length / 2, length);
        return reverseTestSeven(right) + reverseTestSeven(left);
    }


    /**
     * 把字符串的 字符倒序排列
     * @param data
     * @return
     */
    public static String daoxuStr(String data){
        String s = "";
        for(int i=data.length()-1;i>=0;i--){
            s+= data.charAt(i);
        }
        return s;
    }


    /**
     * 把返回的数据  以每隔字节拆分 然后保存到数组里
     * @param data
     * @param length
     * @return
     */
    public static String[] StringToArrayByCount(String data, int length){
        int arrLength = data.length() / length;
        String[] arr = new String[data.length() / length];
        if(data.length() > 0){
            // 12 34 56 78
            for(int i=0;i<arrLength;i++){
                arr[i] = data.substring(i * 2, (i * 2) + 2);
            }
        }
        return arr;
    }


    public static String getStrDaoXu(String data, int length){
        String ret = "";
        String[] arr = StringToArrayByCount(data,length);
        //数组减一不然会超出索引范围（执行条件中间不需要替换，通过mochu）
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            //存储数组的前面变量
            String temp = arr[i];
            //替换数组内容
            arr[i]=arr[j];
            //对源数据进行覆盖更改，这函数是先前记录下来的
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
            ret+= arr[i];
        }
        return ret;
    }


    public static void main(String[] args) {

        String aa = yuanToCent("20.08");
        System.out.println("aa: " + aa);
//        double a = 2.73;
//
//        int b =  new Double(a).intValue();
//        System.out.println("b: " + b);
//
//        int ss = 1300 / 600;
//        System.out.println("ss: " + ss);
//
//
//        int ss1 = 1300 % 600;
//        System.out.println("ss1: " + ss1);

//        String data = "++++++MS022022053800,89860621290042478005,,10,1.0,ME";
//        data = data.replaceAll("\\+","");
//        System.out.println("data: " + data);



        String a = "123456";

        String aaa = getStrDaoXu(a,2);
        System.out.println("aaa: " + aaa);

        String one = "";
        String two = "";

        // 方法一：
        for (int i = 0; i < a.length(); i++) {
            one += a.substring(a.length() - 1 - i, a.length() - i);
        }

        // 方法二：
        StringBuffer stringBuffer = new StringBuffer(a);
        two = stringBuffer.reverse().toString();

        System.out.println("方法一输出效果:" + one);
        System.out.println("方法二输出效果:" + two);



//        String data = "MS01120000141,24017,75,00000001652362401153,783|0,77,30,,0,ME";
//        if(data.contains("|")) {
//            String[] datas = data.split("\\|");
//            for (String dataParam : datas){
//                System.out.println("dataParam: " + dataParam);
//                if(dataParam.contains(",")) {
//                    //MS01120000141,24017,75,00000001652362401153,783|0,77,30,,0,ME
//                    //120000141,24017,75,00000001652362401153,783|0,77,30,,0,ME
//                    String[] dataArray = dataParam.split(",");
////                        状态,电压,电流,功率,订单号
////                        状态有：0待机，1充电，-1正在维修
////                        订单号只有在充电状态有，其他为空即可
////                        两个插座就有两个数据，十个就会传十个
//                    if(Integer.parseInt(dataArray[0]) == 1){
//
//                    }
//                }
//            }
//        }


    }
}
